﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace InstallManager.Msi
{
    public enum MsiProgressMessageType
    {
        Reset = 0,
        ActionInfo = 1,
        ProgressReport = 2,
        TotalTickCountAdjustment = 3

    }
    public enum MsiProgressDirection
    {
        Forward = 0,
        Backward = 1
    }

    public class MsiProgressMessage
    {
        public string Field1 { get; private set; }
        public string Field2 { get; private set; }
        public string Field3 { get; private set; }
        public string Field4 { get; private set; }

        public MsiProgressMessage(string message)
        {
            if (!string.IsNullOrEmpty(message))
            {
                //Regex regex = new Regex("1: (?<field1>[\\d]+) 2: (?<field2>[\\d]+) 3: (?<field3>[\\d]+) 4: (?<field4>[\\d]+)");
                Regex regex = new Regex("1: (?<field1>[\\d]+) ");
                if (regex.IsMatch(message))
                {
                    Match match = regex.Match(message);
                    Field1 = match.Groups["field1"].Value;
                }
                regex = new Regex("2: (?<field2>[\\d]+) ");
                if (regex.IsMatch(message))
                {
                    Match match = regex.Match(message);
                    Field2 = match.Groups["field2"].Value;
                }
                regex = new Regex("3: (?<field3>[\\d]+) ");
                if (regex.IsMatch(message))
                {
                    Match match = regex.Match(message);
                    Field3 = match.Groups["field3"].Value;
                }
                regex = new Regex("4: (?<field4>[\\d]+) ");
                if (regex.IsMatch(message))
                {
                    Match match = regex.Match(message);
                    Field4 = match.Groups["field4"].Value;
                }
            }
        }

        public override string ToString()
        {
            return String.Format("ProgressMessageType:{0} Value:{1} ProgressDirection:{2} NewState:{3}", ProgressMessageType, Value, Direction, NewState);
        }

        public MsiProgressMessageType? ProgressMessageType
        {
            get
            {
                switch (Field1)
                {
                    case "0":
                        return Msi.MsiProgressMessageType.Reset;
                    case "1":
                        return Msi.MsiProgressMessageType.ActionInfo;
                    case "2":
                        return Msi.MsiProgressMessageType.ProgressReport;
                    case "3":
                        return Msi.MsiProgressMessageType.TotalTickCountAdjustment;
                    default:
                        return null;
                }
            }
        }

        public int? Value
        {
            get
            {
                int ttc;
                if (int.TryParse(Field2, out ttc))
                    return ttc;
                return null;
            }
        }

        public MsiProgressDirection? Direction
        {
            get
            {
                switch (Field3)
                {
                    case "0":
                        return MsiProgressDirection.Forward;
                    case "1":
                        return MsiProgressDirection.Backward;

                    default:
                        return null;
                }
            }
        }
        public bool? NewState
        {
            get
            {
                switch (Field3)
                {
                    case "1":
                        return true;
                    case "0":
                        return false;

                    default:
                        return null;
                }
            }
        }
    }
}
